|
|
|
|
|
|
|
If the known pixels have values V11, V12, V21, and V22, interpolating horizontally between pixels (x1, y1) and (x2, y1), and pixels (x1, y2) and (x2, y2) gives the two values: |
|
|
|
 |
|
|
|
|
V1 = (V11 * dx2 + V21 * dx1) / (dx1 + dx2)
V2 = (V12 * dx2 + V22 * dx1) / (dx1 + dx2) |
|
|
|
|
|
|
|
|
Interpolating these values vertically gives: |
|
|
|
 |
|
|
|
|
V = [(V11 * dx2 + V21 * dx1) / (dx1 + dx2) * dy2 +
(V12 * dx2 + V22 * dx1) / (dx1 + dx2) * dy1)] / (dy1 + dy2)
= [V11 * dx2 * dy2 + V21 * dx1 * dy2 +
V12 * dx2 * dy1 + V22 * dx1 * dy1]
/ (dx1 + dx2) / (dy1 + dy2) |
|
|
|
|
|
|
|
|
Because (x1, y1), (x1, y2), (x2, y1), and (x2, y2) are adjacent pixels, dx1 + dx2 = 1 and dy1 + dy2 = 1. Making these substitutions gives: |
|
|
|
 |
|
|
|
|
V = V11 * dx2 * dy2 + V21 * dx1 * dy2 +
V12 * dx2 * dy1 + V22 * dx1 * dy1 |
|
|
|
|
|
|
|
|
A Transformation Framework |
|
|
|
|
|
|
|
|
Using bilinear interpolation, you can build a program that implements a general transformation framework. For each pixel in the output image, the program maps that pixel back to an input pixel location. If that location lies within the input image, the program then uses interpolation to calculate the pixel's value. |
|
|
|
|
|
|
|
|
Example program Trans demonstrates this technique. Its TransformImage subroutine provides the general framework. For each output pixel, it invokes subroutine MapPixel to map the pixel back to an input location. It then uses bilinear interpolation to calculate the value for the output pixel. |
|
|
|
|
|
|
|
|
The following code shows the most interesting parts of the TransformImage subroutine. The longest and least interesting parts have been omitted. You can find the complete source code on the CD-ROM. |
|
|
|
 |
|
|
|
|
' Transform the image. Private Sub TransformImage(ByVal pic_from As PictureBox, _
ByVal pic_to As PictureBox)
Dim input_pixels() As RGBTriplet
Dim result_pixels() As RGBTriplet
Dim bits_per_pixel As Integer
Dim ix_max As Single
Dim iy_max As Single
Dim x_in As Single
Dim y_in As Single |
|
|
|
|
|